package com.case6.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.case5.pojo.BaseDataPojo;
import com.case5.service.LoginService;
import com.case5.util.HttpUtil;
import com.google.gson.Gson;
import com.utils.QiniuUtil;

/**
 * Servlet implementation class Login
 */
@WebServlet("/getQiniuToken")
public class GetQiniuToken extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GetQiniuToken() {
        super();
    }


	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}


	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 设置前后端交互参数的编码为utf-8		
				req.setCharacterEncoding("utf-8");
				resp.setContentType("text/html;charset=utf-8");
				// 用PrintWriter对象返回数据
				PrintWriter out = resp.getWriter();
				
				String token = null;
				// 获取小程序端传过来的参数，key:用于图片命名的参数
				String key = req.getParameter("key");
				System.out.println("key="+key);
				// 获取上传凭证
				token = QiniuUtil.getToken(key);
				System.out.println("token="+token);
				Map<String,String> map = new HashedMap<>();
				map.put("token", token);
				if(token!=null) {
					out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
							String>>("获取成功", true, map)));
				}else {
					out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
							String>>("获取失败", false, map)));
				}
				
		}
	
}
